<template>
  <div class="good-list">
    <!-- 筛选面板 -->
    <el-row :gutter="20">
      <el-col :span="5">
        <el-input v-model="UserId" placeholder="请输入用户ID" />
      </el-col>
      <el-col :span="5">
        <el-input v-model="Dindan" placeholder="请输入订单编号" />
      </el-col>
      <el-col :span="5">
        <el-select v-model="value" placeholder="请选择订单状态">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">查找</el-button>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table id="tableId" :data="tableData" border style="width: 100%">
      <el-table-column align="center" prop="id" label="订单编号" sortable />
      <el-table-column align="center" prop="userId" label="用户ID" />
      <el-table-column align="center" label="订单状态">
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.orderStatus === 103 ? '未支付' : '已支付' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="goodsPrice" label="订单金额" />
      <el-table-column align="center" prop="grouponPrice" label="支付金额" />
      <el-table-column align="center" prop="desc" label="支付时间" />
      <el-table-column align="center" prop="desc" label="物流单号" />
      <el-table-column align="center" prop="desc" label="物流渠道" />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetail(scope.$index, scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 详情 -->


    <el-dialog title="订单详情" :visible.sync="dialogFormVisible">
      <h3>订单编号<span>{{ form.orderSn }}</span></h3>
      <h3>订单用户<span>微信用户</span></h3>
      <h3>用户留言<span></span></h3>
      <h3>收货信息<span>(收货人){{ user.nickname }}(手机号){{ form.mobile
      }}(地址){{ form.address }}</span>
      </h3>
      <h3>商品信息
        <el-table :data="orderGoods">
          <el-table-column align="center" prop="goodsName" label="商品名称"></el-table-column>
          <el-table-column align="center" prop="goodsId" label="商品编号"></el-table-column>
          <el-table-column align="center" prop="specifications[0]" label="货品规格"></el-table-column>
          <el-table-column align="center" prop="price" label="货品价格"></el-table-column>
          <el-table-column align="center" prop="number" label="货品数量"></el-table-column>
          <el-table-column align="center" label="货品图片">
            <template slot-scope="scope">
              <img width="100%" height="100%" :src="scope.row.picUrl" alt="">
            </template>
          </el-table-column>
        </el-table>
      </h3>
      <h3>费用信息<span>(实际费用)=(商品总价)元+(快递费用)元-(优惠减免)元-(积分减免)元</span></h3>
      <h3>支付信息<span>(支付渠道) 微信支付 (支付时间)</span></h3>
      <h3>快递信息<span>(快递公司) (快递单号) (发货时间)</span></h3>
      <h3>收货信息<span>(确认收货信息)</span></h3>

    </el-dialog>
    <!-- 分页 -->
    <el-pagination style="margin-top: 30px" :current-page="currentPage" :page-sizes="[5, 10, 20]" :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />

  </div>
</template>
<script>
import FileSaver from 'file-saver'
import * as XLSX from 'xlsx'
import { OrderList, OrderDetail } from '../../api/ml-user'

export default {
  name: 'Dindan',
  props: [],
  data() {
    return {
      UserId: '',
      Dindan: '',
      tableData: [],
      form: {},
      orderGoods: {},
      user: {},
      total: 0, // 总条数
      currentPage: 1, // 当前页码
      pageSize: 20, // 每页的数据条数
      sort: 'add_time',
      order: 'desc',
      formLabelWidth: '120px',
      dialogFormVisible: false,
      dialogTableVisible: true,
      // ______________
      options: [{
        value: '选项1',
        label: '未付款'
      }, {
        value: '选项2',
        label: '用户取消'
      }, {
        value: '选项3',
        label: '系统取消'
      }, {
        value: '选项4',
        label: '已支付'
      }, {
        value: '选项5',
        label: '申请退款'
      }, {
        value: '选项6',
        label: '已退货'
      }, {
        value: '选项7',
        label: '已发货'
      }, {
        value: '选项8',
        label: '用户收货'
      }, {
        value: '选项9',
        label: '系统收货'
      }],
      value: '',
    }
  },
  created() {
    this.getData(this.currentPage, this.pageSize, this.sort, this.order)
  },
  methods: {
    // 详情
    handleDetail(index, row) {
      // console.log(row);
      this.dialogFormVisible = true
      OrderDetail({ id: row.id }).then(res => {
        console.log(res);
        if (res.errno === 0) {
          this.form = res.data.order;
          this.orderGoods = res.data.orderGoods;
          this.user = res.data.user
        }
      })
    },
    // 获取数据
    getData(n1, n2, n3, n4) {
      OrderList({ page: n1, limit: n2, sort: n3, order: n4 }).then(res => {
        console.log(res)
        if (res.errno == 0) {
          this.tableData = res.data.items,
            this.total = res.data.total
        }
      })
    },

    // 点击导出
    handleExport() {
      var xlsxParam = { raw: true }
      const fix = document.querySelector('.el-table__fixed')
      let wb
      // 判断有无fixed定位，如果有的话去掉，后面再加上，不然数据会重复
      if (fix) {
        wb = XLSX.utils.table_to_book(
          document.querySelector('#tableId').removeChild(fix), xlsxParam
        )
        document.querySelector('#tableId').appendChild(fix)
      } else {
        wb = XLSX.utils.table_to_book(document.querySelector('#tableId'), xlsxParam)
      }
      var wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
      })
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream' }),
          '导出详情单.xlsx'
        ) // 文件名
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
      return wbout
    },

    // 点击搜索
    handleSearch() {
      if (this.UserId != '') {
        var newtabData1 = []
        this.tableData.forEach(item => {
          if (item.userId == this.UserId) {
            newtabData1.push(item)
          }
        })
        this.tableData = newtabData1
        if (this.Dindan != '') {
          var newtabData2 = []
          this.tableData.forEach(item => {
            if (item.orderSn == this.Dindan) {
              newtabData2.push(item)
            }
          })
          this.tableData = newtabData2
        }
      } else {
        if (this.Dindan != '') {
          var newtabData3 = []
          this.tableData.forEach(item => {
            if (item.orderSn == this.Dindan) {
              newtabData3.push(item)
            }
          })
          this.tableData = newtabData3
        } else {
          this.getData()
        }
      }
    },
    // 分页————————
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getData(1, val, this.sort, this.order)
      this.currentPage = 1
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getData(this.currentPage, this.pageSize, this.sort, this.order)

    }
  }
}
</script>

<!-- scss -->
<style lang="scss" scoped>
.good-list {
  padding: 20px;
  box-sizing: border-box;

  .el-table {
    margin-top: 20px;

    .pic {
      height: 80px;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  width: 178px;
  height: 178px;
  overflow: hidden;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

span {
  padding-left: 20px;
  font-size: 13px;
  color: #8c939d;
  font-weight: 100;
}
</style>